/*
 *  NFCSigning - Open source library for signing/validation of NDEF messages
 *  Copyright (C) 2009-2010 The NFCSigning Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */
package org.nfcsigning.algorithm;

import org.nfcsigning.record.SignatureField;
import org.nfcsigning.record.SignatureRecord;
import org.bouncycastle.asn1.DERObjectIdentifier;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;

//#ifdef MyJSR257
//# import my.javax.microedition.contactless.ndef.NDEFRecordType;
//#else
import javax.microedition.contactless.ndef.NDEFRecordType;
//#endif

/**
 *
 * @author Markus Kilås
 */
public class DefaultVerifierFactory extends VerifierFactory {

//    private Hashtable verifiers = new Hashtable(5);
    
    public DefaultVerifierFactory() {
    }

//    
//    public static SignatureFactory getInstance() {
//        if(instance == null) {
//            instance = new SignatureFactory();
//        }
//        return instance;
//    }
//    
    
    public SignatureVerifier getVerifier(NDEFRecordType signatureRecordType, int signatureType) {
        
        SignatureVerifier verifier = null;

        switch(signatureType) {
            case SignatureField.RSASSA_PSS: {
                verifier = RSASSAPSSVerifier.getInstance();
                break;
            }
            case SignatureField.RSASSA_PKCS1_V1_5_WITH_SHA1: {
                if(ImplementationDetector.getInstance().isSatsaAvailable()) {
                    verifier = RsaSsaPkcs1V15SatsaVerifier.getInstance();
                }

                if(verifier == null) {
                    verifier = RsaSsaPkcs1v15BcVerifier.getInstance();
                }
                break;
            }
            case SignatureField.DSA: {
                if(ImplementationDetector.getInstance().isSatsaAvailable()) {
                    verifier = DSASatsaVerifier.getInstance();
                }

                if(verifier == null) {
                    verifier = DSAVerifier.getInstance();
                }
                break;
            }
            case SignatureField.ECDSA_P_192_SHA1: {
                verifier = ECDSAVerifier.getInstance();
                break;
            }
        }
        
        return verifier;
    }

    public SignatureVerifier getVerifier(DERObjectIdentifier algorithmObjectId) {
        if (PKCSObjectIdentifiers.sha1WithRSAEncryption.equals(algorithmObjectId)) {
            return getVerifier(SignatureRecord.RECORD_TYPE, SignatureField.RSASSA_PKCS1_V1_5_WITH_SHA1);
        } else if (ECDSAVerifier.SHA1withECDSA.equals(algorithmObjectId)) {
            return getVerifier(SignatureRecord.RECORD_TYPE, SignatureField.ECDSA_P_192_SHA1);
        } else if(DSAVerifier.id_dsa_with_sha1.equals(algorithmObjectId)) {
            return getVerifier(SignatureRecord.RECORD_TYPE, SignatureField.DSA);
        } else if(PKCSObjectIdentifiers.id_RSASSA_PSS.equals(algorithmObjectId)) {
            return getVerifier(SignatureRecord.RECORD_TYPE, SignatureField.RSASSA_PSS);
        }
        return null;
    }
    
}
